import java.awt.Color;
import java.awt.LayoutManager;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
 * Create The histogram graph in a panel   
 *
 */
public class histogram extends JPanel {

	private static final long serialVersionUID = 1L;
	private JPanel jmainPanel = null;
	/**
	 * x - Start point of all the shapes
	  */
	int x;
	/**
	 * y - Start point of all the shapes 
	 */
	int y;
	int i;
	/**
	 * num_of_bar - number of bars in the histogram graph 
	 */
	int num_of_bar;
	/**
	 * bar_width - bar width in the histogram graph 
	 */
	int bar_width;
	/**
	 * space - space between bars in the histogram graph 
	 */
	int space;
	/**
	 * max_size - the size of the biggest bar in the histogram graph 
	 */
	int max_size;
	/**
	 * bar - color for the first type of bars
	 */
	Color bar;
	/**
	 * bar1 - color for the second type of bars 
	 */
	Color bar1;
	/**
	 * bar_height - sizes of the bars from the first type
	 */
	int[] bar_height;
	/**
	 * bar_height1 - sizes of the bars from the second type 
	 */
	int[] bar_height1;
	/**
	 * lineTitle - sizes of the lines in the histogram graph
	 */
	int[] lineTitle;
	/**
	 * panel_width - panel width
	*/
	int panel_width;
	/**
	 * string_space - space between Strings 
	 */
	int string_space;
	/**
	  * bar_space - space between bars 
	 */
	int bar_space;
	/**
	 * panel_height - panel height
	 */
	int panel_height;
	/**
	 * bar_title - Array of strings the name of each bar
	 */
	String[] bar_title; 
	/**
	 * yTitle - Title for y axis
	 */
	String yTitle;
	/**
	 * xTitle - Title for x axis 
	 */
	String xTitle;
	/**
	 * paneltitle - panel title 
	 */
	String paneltitle;
	/**
	 * bar_name - The name of the first bars type 
	 */
	String bar_name;
	/**
	 * * bar_name1 - The name of the second bars type
	 */
	String bar_name1;
	private JLabel jxLine = null;
	private JLabel jyLine = null;
	private JLabel jyLine150 = null;
	private JLabel jyLine100 = null;
	private JLabel jyLine50 = null;
	private JLabel jyLine200 = null;
	private JLabel j50 = null;
	private JLabel j150 = null;
	private JLabel j100 = null;
	private JLabel j200 = null;
	private JLabel jXtitle = null;
	private JLabel jYtitle = null;
	private JLabel jTitle = null;
	private JLabel jlblaBarName = null;
	private JLabel jlblbarColor = null;
	private JLabel lblBarName1 = null;
	private JLabel jlblbarColor1 = null;
	public JButton btnClose = null;
	/**
	 * Create and initializes histogram
	 * @param num Number of bars.
	 * @param width Width of each bar.
	 * @param sp Space between bars.
	 * @param bg The first bars type color.
	 * @param bg1 The second bars type color.
	 * @param height The first bars type sizes.
	 * @param height1 The second bars type sizes.
	 * @param name Names for all bars.
	 * @param y Title for y axis
	 * @param x Title for x axis
	 * @param title Title for the histogram
	 * @param panel_width1 panel width
	 * @param panel_height1 panel height
	 * @param bar_name_tmp The first bars type title.
	 * @param bar_name_tmp1 The second bars type title.
	 */
	public histogram(int num,int width,int sp,Color bg,Color bg1,int[] height,int[] height1,String[] name,String y,String x,String title,int panel_width1,int panel_height1,String bar_name_tmp,String bar_name_tmp1) {
		// TODO Auto-generated constructor stub
		super();
		lineTitle=new int[4];
		lineTitle[0]=50;
		lineTitle[1]=100;
		lineTitle[2]=150;
		lineTitle[3]=200;
		num_of_bar=num;
		bar_width=width;
		space=sp;
		bar=bg;
		bar1=bg1;
		bar_height=height;
		bar_height1=height1;
		bar_title=name;
		yTitle=y;
		xTitle=x;
		paneltitle=title;
		panel_width=panel_width1;
		panel_height=panel_height1;
		bar_name=bar_name_tmp;
		bar_name1=bar_name_tmp1;
		max_size=findMax(height, 0);
		max_size=findMax(height1, max_size);
		setNew(height, max_size);
		setNew(height1, max_size);
		if(max_size%2==0)
		{
			lineTitle[0]=max_size/4;
			lineTitle[1]=2*lineTitle[0];
			lineTitle[2]=3*lineTitle[0];
			lineTitle[3]=max_size;
			if(lineTitle[0]==0)
				if(max_size%2==0)
					lineTitle[1]=max_size/2;
		}
		else
		{
			lineTitle[0]=0;
			lineTitle[1]=2*lineTitle[0];
			lineTitle[2]=3*lineTitle[0];
			lineTitle[3]=max_size;
		}
		initialize() ;
		
	}

	public histogram(LayoutManager arg0) {
		super(arg0);
		// TODO Auto-generated constructor stub
		initialize();
	}

	public histogram(boolean arg0) {
		super(arg0);
		// TODO Auto-generated constructor stub
		initialize();
	}

	public histogram(LayoutManager arg0, boolean arg1) {
		super(arg0, arg1);
		// TODO Auto-generated constructor stub
		initialize();
	}

	/**
	 * initializes histogram
	 * 
	 * @return void
	 */
	private void initialize() {
		jlblbarColor1 = new JLabel();
		jlblbarColor1.setBounds(new Rectangle(panel_width/2+50+labelSize(bar_name1), 75+panel_height, 20, 20));
		jlblbarColor1.setOpaque(true);
		jlblbarColor1.setBackground(bar1);
		lblBarName1 = new JLabel();
		lblBarName1.setBounds(new Rectangle(panel_width/2+30, 72+panel_height, labelSize(bar_name1), 26));
		lblBarName1.setText(bar_name1);
		jlblbarColor = new JLabel();
		jlblbarColor.setBounds(new Rectangle(panel_width/2+50+labelSize(bar_name), 55+panel_height, 20, 20));
		jlblbarColor.setOpaque(true);
		jlblbarColor.setBackground(bar);
		jlblaBarName = new JLabel();
		jlblaBarName.setBounds(new Rectangle(panel_width/2+30, 52+panel_height, labelSize(bar_name), 31));
		jlblaBarName.setText(bar_name);
		jTitle = new JLabel();
		jTitle.setBounds(new Rectangle(panel_width/2+50, 15, 216, 26));
		jTitle.setText(paneltitle);
		this.setSize(panel_width+100, panel_height+200);
		this.setLayout(null);
		x=25+3+3+8*yTitle.length();
		y=3+3;
		this.add(getJmainPanel(), null);
		this.add(jTitle, null);
		this.add(jlblaBarName, null);
		this.add(jlblbarColor, null);
		this.add(lblBarName1, null);
		this.add(jlblbarColor1, null);
		this.add(getBtnClose(), null);
		
		
	}

	/**
	 * Return the max String from String array.
	 * @param data String array
	 * @return The Length of the longest string
	 */
	public int maxString(String[] data)
	{
		int max=0,i;
		for(i=0;i<data.length;i++)
		{
			if(data[i].length()>max)
				max=data[i].length();
		}
		return max;
	}
	/**
	 * Calculate label size for st String 
	 * @param st String which will be in a label
	 * @return label size
	 */
	static public int labelSize(String st)
	{
		return 8*st.length();
	}
	/**
	 * Create and draw the histogram.
	 * @return JPanel holds the histogram.
	 */
	private JPanel getJmainPanel() {
		if (jmainPanel == null) {
			JLabel jarray[],jarray1[],jString[];
			//panel_width=25+5+3+5+labelSize(yTitle)+labelSize(xTitle);
			jarray=new JLabel[num_of_bar];
			jarray1=new JLabel[num_of_bar];
			jString=new JLabel[num_of_bar]	;
			int xyline=25+8*yTitle.length();
			x=xyline+space;
			for(i=0;i<num_of_bar;i++)
			{
				jarray[i]=new JLabel();
				jarray1[i]=new JLabel();
				jString[i]=new JLabel();
				int bar_space_tmp,string_space_tmp,x_move;
				if(labelSize(bar_title[i])>2*bar_width)
				{
					bar_space_tmp=(labelSize(bar_title[i])-2*bar_width)/2;
					string_space_tmp=0;
					x_move=labelSize(bar_title[i]);
				}
				else
				{
					bar_space_tmp=0;
					string_space_tmp=(2*bar_width-labelSize(bar_title[i]))/2;
					x_move=2*bar_width;
				}
				jarray[i].setBounds(new Rectangle(x+bar_space_tmp, 200+y-bar_height[i], bar_width, bar_height[i]));
				jarray1[i].setBounds(new Rectangle(x+bar_space_tmp+bar_width, 200+y-bar_height1[i], bar_width, bar_height1[i]));
				jString[i].setBounds(new Rectangle(x+string_space_tmp, 210, labelSize(bar_title[i]), 10));
				jString[i].setText(bar_title[i]);
				jString[i].setHorizontalAlignment(JLabel.CENTER);
				jarray[i].setOpaque(true);
				jarray1[i].setOpaque(true);
				jarray[i].setBackground(bar);
				jarray1[i].setBackground(bar1);
				x=x+x_move+space;
				//panel_width=panel_width+x_move+space;
			}
			jyLine200 = new JLabel();
			jyLine200.setBounds(new Rectangle(25+8*yTitle.length(), 5, panel_width-10, 1));
			jyLine200.setOpaque(true);
			jyLine200.setBackground(Color.black);
			j200=new JLabel();
			j200.setBounds(new Rectangle(10+8*yTitle.length(), 5, 15, 10));
			j200.setText(Integer.toString(lineTitle[3]));
			jyLine150 = new JLabel();
			jyLine150.setBounds(new Rectangle(25+8*yTitle.length(), 55, panel_width-10, 1));
			jyLine150.setOpaque(true);
			jyLine150.setBackground(Color.black);
			j150=new JLabel();
			j150.setBounds(new Rectangle(10+8*yTitle.length(), 50, 15, 10));
			j150.setText(Integer.toString(lineTitle[2]));
			jyLine100 = new JLabel();
			jyLine100.setBounds(new Rectangle(25+8*yTitle.length(), 105, panel_width-10, 1));
			jyLine100.setOpaque(true);
			jyLine100.setBackground(Color.black);
			j100=new JLabel();
			j100.setBounds(new Rectangle(10+8*yTitle.length(), 100, 15, 10));
			j100.setText(Integer.toString(lineTitle[1]));
			jyLine50 = new JLabel();
			jyLine50.setBounds(new Rectangle(25+8*yTitle.length(), 155, panel_width-10, 1));
			jyLine50.setOpaque(true);
			jyLine50.setBackground(Color.black);
			j50=new JLabel();
			j50.setBounds(new Rectangle(10+8*yTitle.length(), 150, 10, 10));
			j50.setText(Integer.toString(lineTitle[0]));
			jyLine = new JLabel();
			jyLine.setBounds(new Rectangle(xyline, 5, 3, 203));
			jyLine.setOpaque(true);
			jyLine.setBackground(Color.black);
			jxLine = new JLabel();
			jxLine.setBounds(new Rectangle(25+8*yTitle.length(), 205, panel_width-5-(25+8*yTitle.length()), 3));
			jxLine.setOpaque(true);
			jxLine.setBackground(Color.black);
			jXtitle=new JLabel();
			jXtitle.setBounds(new Rectangle(panel_width-8*xTitle.length()-5, 210, 8*xTitle.length(), 10));
			jXtitle.setText(xTitle);
			jYtitle=new JLabel();
			jYtitle.setBounds(new Rectangle(5, 5, 8*yTitle.length(), 10));
			jYtitle.setText(yTitle);
			jmainPanel = new JPanel();
			jmainPanel.setLayout(null);
			jmainPanel.setBounds(new Rectangle(46, 42, panel_width, panel_height));
			jmainPanel.setBorder(BorderFactory.createLineBorder(Color.gray, 5));
			jmainPanel.add(jxLine, null);
			jmainPanel.add(jyLine, null);
			jmainPanel.add(jyLine200, null);
			jmainPanel.add(jyLine150, null);
			jmainPanel.add(jyLine100, null);
			jmainPanel.add(jyLine50, null);
			jmainPanel.add(j200, null);
			jmainPanel.add(j150, null);
			jmainPanel.add(j100, null);
			jmainPanel.add(j50, null);
			jmainPanel.add(jXtitle, null);
			jmainPanel.add(jYtitle, null);
						jmainPanel.addMouseListener(new java.awt.event.MouseAdapter() { 
							public void mouseClicked(MouseEvent e) {    
								System.out.println("x="+ e.getX() + ",y="+e.getY()); // TODO Auto-generated Event stub mouseClicked()
							}
						});
			for(i=0;i<num_of_bar;i++)
			{
				jmainPanel.add(jarray[i], null);
				jmainPanel.add(jarray1[i], null);
				jmainPanel.add(jString[i], null);
			}
			if(lineTitle[0]==0)
			{
				jyLine50.setVisible(false);
				j50.setVisible(false);
			}
			if(lineTitle[1]==0)
			{
				jyLine100.setVisible(false);
				j100.setVisible(false);
			}
			if(lineTitle[2]==0)
			{
				jyLine150.setVisible(false);
				j150.setVisible(false);
			}
		}
		return jmainPanel;
	}

	/**
	 *initializes btnClose	
	 * 	
	 * @return btnClose JButton
	 */
	private JButton getBtnClose() {
		if (btnClose == null) {
			btnClose = new JButton();
			btnClose.setBounds(new Rectangle(panel_width-10, 100+panel_height, 100, 40));
			btnClose.setText("Close");
			
		}
		return btnClose;
	}
	/**
	 * Find the maximum Integer from Integer array and from anther Integer
	 * @param data Integer array
	 * @param max anther Integer
	 * @return The maximum from the Integer array and anther Integer
	 */
	public int findMax(int[] data,int max)
	{
		int i;
		for(i=0;i<data.length;i++)
		{
			if(data[i]>max)
				max=data[i];
		}
		return max;
	}
	/**
	 * Change the size of the bars, so the maximum bar will be 200 Pixels
	 * @param data array of int
	 * @param max The maximum int in the array.
	 */
	public void setNew(int[] data,int max)
	{
		int i;
		for(i=0;i<data.length;i++)
		{
			data[i]=data[i]*200;
			data[i]=data[i]/max;
			
		}
	}
	

} 
